﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace _3DWorldOfSmith
{
    class WindmillFans : GameObject
    {
        GraphicsDevice device;

        VertexPositionColor[] fansVertices;

        VertexBuffer buffer;
        BasicEffect effect;

        public WindmillFans(GraphicsDevice device)
        {
            this.device = device;

            Start();
        }

        public override void SetUpVertices()
        {
            this.fansVertices = new VertexPositionColor[12];
            #region Vertices
            // First fan
            this.fansVertices[0] = new VertexPositionColor(new Vector3(position.X, position.Y, position.Z), Color.Gray);
            this.fansVertices[1] = new VertexPositionColor(new Vector3(position.X - size.X, position.Y + size.Y/2, position.Z), Color.Gray);
            this.fansVertices[2] = new VertexPositionColor(new Vector3(position.X - size.X, position.Y - size.Y/2, position.Z), Color.Gray);
            // Second fan
            this.fansVertices[3] = new VertexPositionColor(new Vector3(position.X, position.Y, position.Z), Color.Gray);
            this.fansVertices[4] = new VertexPositionColor(new Vector3(position.X + size.X, position.Y + size.Y / 2, position.Z), Color.Gray);
            this.fansVertices[5] = new VertexPositionColor(new Vector3(position.X - size.X, position.Y + size.Y / 2, position.Z), Color.Gray);
            // Third fan
            this.fansVertices[6] = new VertexPositionColor(new Vector3(position.X, position.Y, position.Z), Color.Gray);
            this.fansVertices[7] = new VertexPositionColor(new Vector3(position.X + size.X, position.Y - size.Y / 2, position.Z), Color.Gray);
            this.fansVertices[8] = new VertexPositionColor(new Vector3(position.X + size.X, position.Y + size.Y / 2, position.Z), Color.Gray);
            // Fourth fan
            this.fansVertices[9] = new VertexPositionColor(new Vector3(position.X, position.Y, position.Z), Color.Gray);
            this.fansVertices[10] = new VertexPositionColor(new Vector3(position.X - size.X, position.Y - size.Y / 2, position.Z), Color.Gray);
            this.fansVertices[11] = new VertexPositionColor(new Vector3(position.X + size.X, position.Y - size.Y / 2, position.Z), Color.Gray);
            #endregion
        }

        public override void Start()
        {
            position = new Vector3(0, 0, 0);
            rotation = new Vector3(0, 0, 0);
            size = new Vector3(1, 1, 1);

            SetUpVertices();

            this.buffer = new VertexBuffer(device, typeof(VertexPositionTexture), fansVertices.Length , BufferUsage.None);
            this.effect = new BasicEffect(device);
        }

        public override void Update()
        {
            SetUpVertices();

            world = Matrix.CreateRotationX(rotation.X) *
                    Matrix.CreateRotationY(rotation.Y) *
                    Matrix.CreateRotationZ(rotation.Z);
        }

        public override void Draw()
        {
            effect.World = world;
            effect.Projection = projection;
            effect.View = view;
            effect.TextureEnabled = true;
            effect.Texture = texture;

            device.SetVertexBuffer(buffer);

            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();

                device.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleList, fansVertices, 0, 4);
            }
        }
    }
}
